<template>
	<view>
		<u-navbar title-size="34" title="实名认证" back-icon-color="#36363A" :custom-back="tui.goBack" title-color="#36363A"
			:border-bottom="false" placeholder>
		</u-navbar>
		<view class="container">
			<view v-if="User.status != 1">
				<view class="avatar-box">
					<u-alert-tips :show-icon="true" type="warning" :description="description"></u-alert-tips>
				</view>
				<view class="avatar-box">
					<view class="setting-list setting-border">
						<view class="text-2C2C2C fz-30" style="width: 160upx;">真实姓名</view>
						<view class="d-flex justify-content-right setting-right">
							<input type="text" v-model.trim="name" placeholder-style="color:#D8D8D8; font-size:15px"
								placeholder="真实姓名" />
						</view>
					</view>
				</view>
				<view class="avatar-box">
					<view class="setting-list setting-border">
						<view class="text-2C2C2C fz-30" style="width: 160upx;">身份证号</view>
						<view class="d-flex justify-content-right setting-right">
							<input type="text" v-model.trim="card_no" placeholder-style="color:#D8D8D8; font-size:15px"
								placeholder="本人身份证号" />
						</view>
					</view>
				</view>
				<!-- <view class="upload-padding position-relative">
					<view class="x-auto mb-2 upload-box" @click="uploadImage(1)">
						<image v-if="card_picture_face" :src="card_picture_face" mode="aspectFill" class="upload-box-works">
						</image>
					</view>
				</view>
				<view class="upload-padding position-relative">
					<view class="x-auto mb-2 upload-box" @click="uploadImage(2)"
						style="background-image: url(../../static/setting/Back-img.png);">
						<image v-if="card_picture_back" :src="card_picture_back" mode="aspectFill" class="upload-box-works">
						</image>
					</view>
				</view> -->
				<view class="avatar-box mt-5">
					<u-button :loading="loading"  hover-class="none" class="main-bg family-sc fz-36 mt-2rem mb-2" @tap="identityStore">
						提交</u-button>
				</view>
			</view>
			<view v-else>
				<view class="text-292940 fz-44 text-center verified-text">您已实名认证</view>
				<view class="verified-card">
					<view class="d-flex align-items-center verified-tip">
						<view class="verified-tip-icon">
							<image src="/static/mine/verified-tip.png"></image>
						</view>
						<view>已检测为安全环境，个人信息安全保障中</view>
					</view>
					<view class="d-flex">
						<view class="verified-icon">
							<image src="/static/mine/verified-icon.png"></image>
						</view>
						<view class="text-292940">
							<view class="fz-28 mt-2">姓名</view>
							<view class="font-weight-600 fz-32 line-h-1em name-style">{{nameDesensitization(User.name)}}</view>
							<view class="fz-28 line-h-1em ID-style">身份证号</view>
							<view class="fz-32 font-weight-600">{{User.card_no.substring(0,6)}}********{{User.card_no.substring(14)}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-modal v-model="show" title="确认认证？" :content="content" show-cancel-button  @confirm="confirm" confirmColor="#00C0FF"></u-modal>
	</view>
</template>

<script>
	import ossUpload from '@/common/ossutil/uploadFile.js';
	export default {
		data() {
			return {
				content:'认证成功后，实名信息将无法修改，请核实无误后再提交!',
				card_no: '',
				name: '',
				card_picture_face: '',
				card_picture_back: '',
				loading:false,
				//国内身份证正则
				CNRe18: /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
				description:'请使用本人身份信息，实名认证成功后，身份信息与此账号唯一绑定，不可更换他人。',
				show:false
			}
		},
		computed: {
			User() {
				return this.$store.state.auth.USER
			}
		},
		onLoad() {
			if (this.User.card_no) {
				this.name = this.User.name
				this.card_no = this.User.card_no
				// this.card_picture_face = this.User.card_picture_face
				// this.card_picture_back = this.User.card_picture_back
			}
		},
		methods: {
			//名称脱敏
			nameDesensitization(str){
				if(null == str || str == undefined || str.length == 0){
				  return ''
				}
				if(str.length == 1) {
				  return str
				}
				if(str.length == 2){
					return str.substring(0,1)+'*' //截取name 字符串截取第一个字符，
				}
				
				let starStr = ''
				for (let index = 0; index < str.length - 2; index++) {
				  starStr += "*"
				}
				return str.substring(0, 1) + starStr + str.substring(str.length - 1, str.length)
			},
			confirm(){
				let param = {
					name: this.name,
					card_no: this.card_no,
				}
				this.show = false
				this.loading = true
				this.$store.dispatch('auth/certification', param).then(res => {
					this.loading = false
					uni.$u.toast('提交成功')
					setTimeout(()=>{
						this.$store.dispatch('auth/user').then(res => {
							uni.switchTab({
								url:'/pages/tabBars/mine'
							})
						})
					},1000)
				}).catch(error=>{
					this.loading = false
				})
			},
			// 提交实名认证
			identityStore() {
				if (this.name === '') {
					return uni.$u.toast('请输入真实姓名')
				}
				if (this.card_no === '') {
					return uni.$u.toast('请输入身份证号')
				}
				if (!this.CNRe18.test(this.card_no)) {
					return uni.$u.toast('身份证号码格式错误')
				}
				// if (this.card_picture_face === '') {
				// 	return uni.$u.toast('请上传身份证正面')
				// }
				// if (this.card_picture_back === '') {
				// 	return uni.$u.toast('请上传身份证反面')
				// }
				// let param = {
				// 	name: this.name,
				// 	card_no: this.card_no,
				// 	// card_picture_face: this.card_picture_face,
				// 	// card_picture_back: this.card_picture_back,
				// }
				this.show = true
			},
			//上传作品[图片]
			uploadImage(index) {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					success: (tempRes) => {
						let fileType = ''
						// #ifdef H5
						//网页端获取文件类型
						fileType = tempRes.tempFiles[0].name.substring(tempRes.tempFiles[0].name.lastIndexOf(
							".") + 1);
						// #endif
						//获取上传文件blob地址
						const filePath = tempRes.tempFilePaths[0]
						ossUpload(filePath, fileType, result => {
							if (index == 1) {
								this.card_picture_face = result
							}
							if (index == 2) {
								this.card_picture_back = result
							}
							uni.hideLoading()  
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		padding-top: 30upx;
		box-sizing: border-box;
		background-color: #FFFFFFF;

		button {
			width: 100%;
			height: 100upx;
			border-radius: 50upx;
			line-height: 95upx;
			font-size: 32upx;
			color: #FFFFFF;

		}
		/deep/.u-alert-tips--border--warning-disabled{
			border-color: rgba(000,000,000,0);
		}
		/deep/.u-alert-desc{
			color:#FA8C16;
			line-height: 40upx;
		}
			
		/deep/.u-alert-tips{
			align-items: flex-start;
			height: 125upx;
			padding:24upx 30upx;
		}
		/deep/.u-icon-wrap{
			display:initial;
		}
	}

	.avatar-box {
		width: 100%;
		background-color: #fff;
		padding: 0 30upx;
		box-sizing: border-box;
		margin-bottom: 42upx;
		.setting-list {
			width: 100%;
			height: 140upx !important;
			
			.setting-right {
				height: 92upx;
				align-items: center;

				.user-name {
					font-size: 30upx;
					color: #999999;
				}
			}

			.arrow-icon {
				width: 44upx;
				height: 44upx;

				image {
					width: 44upx;
					height: 44upx;
					vertical-align: top;
				}
			}
		}

		.setting-border {
			border-bottom: 1upx solid #EEEEEE;
		}
	}

	.setting-margin {
		margin-bottom: 20upx;
	}

	.upload-padding {
		padding: 0 0 75upx 0;

		.upIcon {
			position: absolute;
			right: 100upx;
			top: 20upx;
			z-index: 99999;
		}
	}

	/deep/ .u-list-item {
		background: rgba(255, 255, 255, 0) !important;
	}

	/deep/ .u-icon {
		color: rgba(255, 255, 255, 0);
	}

	.success-wrap {
		width: 100%;
		padding: 100upx 0 0 0;
		box-sizing: border-box;

		.success-icon {
			width: 112upx;
			height: 112upx;
			margin-bottom: 49upx;

			image {
				width: 112upx;
				height: 112upx;
				vertical-align: top;
			}
		}

		.padding-30 {
			padding-bottom: 30upx;
		}

		.u-hairline-border[data-v-6e15e680]:after {
			border: none;
		}

		button {
			width: 240upx;
			height: 72upx;
			background: #F7EAFC;
			border-radius: 36upx;
			line-height: 72upx;
			color: #FFFFFF;
			margin-top: 80upx;
		}
	}

	/deep/ .uni-modal__btn_primary {
		color: #AD1BD8 !important;
	}

	.upload-box {
		width: 629upx;
		height: 422upx;
		background: rgba(255, 255, 255, 0);
		background-image: url(../../static/setting/Front-img.png);
		background-repeat: no-repeat;
		background-size: 100% 100%;
		border-radius: 4upx;
		overflow: hidden;
	}

	.slot-btn {
		width: 120upx;
		height: 120upx;
		position: absolute;
		left: 50%;
		transform: translateX(-50%);
		top: 50%;
		top: 95upx;

		image {
			width: 100%;
			height: 100%;
		}

	}
	
	.upload-box-works {
		width: 100%;
		height: 100%;
		border-radius: 4upx;
	}
	.verified-text{
		padding: 48upx 0 58upx 0 ;
	}
	.verified-card{
		width: 690upx;
		margin:0 auto;
		border-radius: 24upx;
		background: url(/static/mine/verified-card.png);
		height: 390upx;
		background-repeat: no-repeat;
		background-size: 100%;
		.verified-tip{
			width: 100%;
			height: 68upx;
			background: linear-gradient(90deg, rgba(232, 242, 254, 0.7) 0%, rgba(251, 253, 255, 0.7) 100%);
			border-radius: 24upx 24upx 0 0 ;
			margin-bottom: 35upx;
			.verified-tip-icon{
				width: 28upx;
				height: 28upx;
				margin-right: 10upx;
				margin-left: 40upx;
				image{
					width: 28upx;
					height: 28upx;
				}
			}
		}
		.verified-icon{
			width: 90upx;
			height: 90upx;
			margin: 0 30upx 0 40upx;
			image{
				width: 90upx;
				height: 90upx;
				vertical-align: top;
			}
		}
		.name-style{
			margin:24upx 0 46upx;
		}
		.ID-style{
			margin: 0 0 24upx 0;
		}
	}
</style>
